const mongoose = require('./mongoose.js');

var Schema = mongoose.Schema;
//帖子
var PostingsSchema = new Schema({
	user_id: {type: Schema.Types.ObjectId, ref: 'User'},	//楼主
	user_head: String,	//头像
	name: String,	//昵称
	title: String,	//标题
	content: String,	//内容
	images: Array,	//图片
	time: String,	//发布时间
	like_count: Number,	//点赞
	comment_id: Array	//评论用户
});
// 评论
var CommentUserSchema = new Schema({
	user_id: {type: Schema.Types.ObjectId, ref: 'User'},	//评论用户
	comment: String	//评论内容
});

var Postings = mongoose.model('Posting', PostingsSchema);
var CommentUser = mongoose.model('CommentUser', CommentUserSchema);


module.exports = {
	getAllPostings: function(callback){
		Postings.find((err, data)=>{
			callback(err, data);
		});
	},
	addPosting: function(obj){
		// {
		// 	user_id: (mongoose.Types.ObjectId)("5ffdf97d57e6395390cfc6cd"),
		// 	user_head: "./head_img/5ffdf97d57e6395390cfc6cd.jpg",
		// 	name: "咸鱼",
		// 	title: "傅亮sb",
		// 	content: "爬爬爬爬爬爬",
		// 	images: ["./imgs/1.jpg", "./imgs/1.jpg", "./imgs/1.jpg"],
		// 	time: "20210114",
		// 	like_count: 0,
		// 	comment_id: [{
		// 		user_id: (mongoose.Types.ObjectId)("000000000000000000000000"),
		// 		name: "靓仔",
		// 		comment: "我赞成"
		// 	},{
		// 		user_id: (mongoose.Types.ObjectId)("000000000000000000000000"),
		// 		name: "靓仔",
		// 		comment: "我赞成"
		// 	},{
		// 		user_id: (mongoose.Types.ObjectId)("000000000000000000000000"),
		// 		name: "靓仔",
		// 		comment: "我赞成"
		// 	}]
		// }
		var p = new Postings(obj);
		p.save();
	},
	deleteUserPosting: function(id, callback){
		Postings.deleteOne({user_id: id}, (err, data)=>{
			callback(data);
		});
	},
	getUserPosting: function(id, callback){
		Postings.findOne({user_id: id}, (err, data)=>{
			callback(data);
		});
	},
	
	deletePostingComment: function(p_id, c_id, callback){
		Postings.findOne({_id: p_id}, (err, data)=>{
			
		});
	},
}